﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
namespace WebApplication1
{
    public partial class DanhMucSach : System.Web.UI.Page
    {
        sv_QuanLyNhaSach.AdapterClient service = new sv_QuanLyNhaSach.AdapterClient();
        string MaTKOL = "";
        string menu = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Browser.Cookies)
            {
                if (Request.Cookies.Get("Login") != null)
                {
                    HttpCookie MyCookies = Request.Cookies["Login"];
                    if (MyCookies["UserName"] != "" || MyCookies["UserName"] != null)
                    {
                        MaTKOL = LayMaTK(MyCookies["UserName"].ToString());
                    }
                }
                else
                    if (Session["UserName"] != null)
                    {
                        MaTKOL = LayMaTK(Session["UserName"].ToString());
                    }
            }
            Load_SachMoi();
            Load_BanChay();
        }
        private string LayMaTK(string p)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(service.selectqueryByText("select matkol from TaiKhoanOnline where TenDangNhap='" + p + "' ")));
            if (ds.Tables.Count > 0)
            {
                return ds.Tables[0].Rows[0][0].ToString();
            }
            return "";
        }
        private void Load_SachMoi()
        {
            DataTable tbl = new DataTable();
            tbl.Columns.Add("MaMH", typeof(string));
            tbl.Columns.Add("TenMH", typeof(string));
            tbl.Columns.Add("TacGia", typeof(string));
            tbl.Columns.Add("NhaSX", typeof(string));
            tbl.Columns.Add("DonGia", typeof(double));
            tbl.Columns.Add("KhuyenMai", typeof(float));
            DataSet ds1 = new DataSet();
            ds1.ReadXml(new StringReader(service.selectqueryByText("select top 1 MaMH,TenMH,TacGia,NhaSX, DonGiaLe,Khuyenmai from MatHang where mamh like '%TT%' order by mamh desc")));
            DataRow dr = tbl.NewRow();
            if (ds1.Tables.Count > 0)
            {
                dr["MaMH"] = ds1.Tables[0].Rows[0]["MaMH"];
                dr["TenMH"] = ds1.Tables[0].Rows[0]["TenMH"];
                dr["TacGia"] = ds1.Tables[0].Rows[0]["TacGia"];
                dr["NhaSX"] = ds1.Tables[0].Rows[0]["NhaSX"];
                dr["DonGia"] = double.Parse(ds1.Tables[0].Rows[0]["DonGiaLe"].ToString().Replace(".0000", ""));
                dr["Khuyenmai"] = float.Parse(ds1.Tables[0].Rows[0]["Khuyenmai"].ToString());
                tbl.Rows.Add(dr);
            }
            DataSet ds2 = new DataSet();
            ds2.ReadXml(new StringReader(service.selectqueryByText("select top 1 MaMH,TenMH,TacGia,NhaSX, DonGiaLe,Khuyenmai from MatHang where mamh like '%KT%' order by mamh desc"))); 
            dr = tbl.NewRow();
            if (ds2.Tables.Count > 0)
            {
                dr["MaMH"] = ds2.Tables[0].Rows[0]["MaMH"];
                dr["TenMH"] = ds2.Tables[0].Rows[0]["TenMH"];
                dr["TacGia"] = ds2.Tables[0].Rows[0]["TacGia"];
                dr["NhaSX"] = ds2.Tables[0].Rows[0]["NhaSX"];
                dr["DonGia"] = double.Parse(ds2.Tables[0].Rows[0]["DonGiaLe"].ToString().Replace(".0000", ""));
                dr["Khuyenmai"] = float.Parse(ds2.Tables[0].Rows[0]["Khuyenmai"].ToString());
                tbl.Rows.Add(dr);
            }
            DataSet ds3 = new DataSet();
            ds3.ReadXml(new StringReader(service.selectqueryByText("select top 1 MaMH,TenMH,TacGia,NhaSX, DonGiaLe,Khuyenmai from MatHang where mamh like '%KI%' order by mamh desc")));
            dr = tbl.NewRow();
            if (ds3.Tables.Count > 0)
            {
                dr["MaMH"] = ds3.Tables[0].Rows[0]["MaMH"];
                dr["TenMH"] = ds3.Tables[0].Rows[0]["TenMH"];
                dr["TacGia"] = ds3.Tables[0].Rows[0]["TacGia"];
                dr["NhaSX"] = ds3.Tables[0].Rows[0]["NhaSX"];
                dr["DonGia"] = double.Parse(ds3.Tables[0].Rows[0]["DonGiaLe"].ToString().Replace(".0000", ""));
                dr["Khuyenmai"] = float.Parse(ds3.Tables[0].Rows[0]["Khuyenmai"].ToString());
                tbl.Rows.Add(dr);
            }
            DataSet ds4 = new DataSet();
            ds4.ReadXml(new StringReader(service.selectqueryByText("select top 1 MaMH,TenMH,TacGia,NhaSX, DonGiaLe,Khuyenmai from MatHang where mamh like '%CN%' order by mamh desc")));
            dr = tbl.NewRow();
            if (ds4.Tables.Count > 0)
            {
                dr["MaMH"] = ds4.Tables[0].Rows[0]["MaMH"];
                dr["TenMH"] = ds4.Tables[0].Rows[0]["TenMH"];
                dr["TacGia"] = ds4.Tables[0].Rows[0]["TacGia"];
                dr["NhaSX"] = ds4.Tables[0].Rows[0]["NhaSX"];
                dr["DonGia"] = double.Parse(ds4.Tables[0].Rows[0]["DonGiaLe"].ToString().Replace(".0000", ""));
                dr["Khuyenmai"] = float.Parse(ds4.Tables[0].Rows[0]["Khuyenmai"].ToString());
                tbl.Rows.Add(dr);
            }
            DataSet ds5 = new DataSet();
            ds5.ReadXml(new StringReader(service.selectqueryByText("select top 1 MaMH,TenMH,TacGia,NhaSX, DonGiaLe,Khuyenmai from MatHang where mamh like '%NN%' order by mamh desc")));
            dr = tbl.NewRow();
            if (ds5.Tables.Count > 0)
            {
                dr["MaMH"] = ds5.Tables[0].Rows[0]["MaMH"];
                dr["TenMH"] = ds5.Tables[0].Rows[0]["TenMH"];
                dr["TacGia"] = ds5.Tables[0].Rows[0]["TacGia"];
                dr["NhaSX"] = ds5.Tables[0].Rows[0]["NhaSX"];
                dr["DonGia"] = double.Parse(ds5.Tables[0].Rows[0]["DonGiaLe"].ToString().Replace(".0000", ""));
                dr["Khuyenmai"] = float.Parse(ds5.Tables[0].Rows[0]["Khuyenmai"].ToString());
                tbl.Rows.Add(dr);
            }
            if (!IsPostBack)
            {
                dl_sachmoi.DataSource = tbl;
                dl_sachmoi.DataBind();
            }

        }
        private void Load_BanChay()
        {
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(service.selectqueryByText("select top 5 MaMH,TenMH,TacGia,NhaSX, DonGiaLe,Khuyenmai from MatHang where MaMH in(select top 5 mamh from CTDatHangONL group by MaMH order by SUM(soluong) desc)")));
            if (ds.Tables.Count>0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    ds.Tables[0].Rows[i]["dongiale"] = ds.Tables[0].Rows[i]["dongiale"].ToString().Replace(".0000","");
                }
                if (!IsPostBack)
                {
                    dl_sachbanchay.DataSource = ds.Tables[0];
                    dl_sachbanchay.DataBind();
                }
            }
        }

        protected void dl_sachbanchay_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "Dat")
            {
                if (MaTKOL == "")
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Bạn chưa đăng nhập.');", true);
                else
                {
                    DataTable tbl;
                    if (Session["GioHang"] == null)
                    {
                        tbl = new DataTable();
                        tbl.Columns.Add("MaMH", typeof(string));
                        tbl.Columns.Add("TenMH", typeof(string));
                        tbl.Columns.Add("SoLuong", typeof(int));
                        tbl.Columns.Add("DonGia", typeof(double));
                        tbl.Columns.Add("KhuyenMai", typeof(float));
                        tbl.Columns.Add("ThanhTien", typeof(double), "SoLuong*DonGia");
                    }
                    else
                        tbl = (DataTable)Session["GioHang"];
                    bool trungma = true;
                    if (tbl.Rows.Count > 0)
                    {
                        for (int i = 0; i < tbl.Rows.Count; i++)
                        {
                            if (tbl.Rows[i]["MaMH"].ToString() == e.CommandArgument.ToString())
                            {
                                trungma = false;
                                tbl.Rows[i]["SoLuong"] = (int.Parse(tbl.Rows[i]["SoLuong"].ToString().Trim()) + 1).ToString();
                            }
                        }
                    }
                    if (trungma == true)
                    {
                        DataRow dr = tbl.NewRow();
                        dr["MaMH"] = e.CommandArgument.ToString();
                        HyperLink hb_tenmh = (HyperLink)e.Item.FindControl("HyperLink1");
                        dr["TenMH"] = hb_tenmh.Text;
                        dr["SoLuong"] = 1;
                        Label lb_dongia = (Label)e.Item.FindControl("Label2");
                        dr["DonGia"] = double.Parse(lb_dongia.Text.Replace("VNĐ", ""));
                        dr["KhuyenMai"] = KM(e.CommandArgument.ToString());
                        tbl.Rows.Add(dr);
                    }
                    Session["GioHang"] = tbl;
                }
            }
        }
        private float KM(string mamh)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(service.selectqueryByText("select khuyenmai from mathang where mamh='" + mamh + "'")));
            float km = float.Parse(ds.Tables[0].Rows[0][0].ToString());
            return km;
        }
    }
}